package com.example.shopdemo.controller.shop;

import com.example.shopdemo.dto.ProductExecution;
import com.example.shopdemo.entity.Product;
import com.example.shopdemo.entity.ProductCategory;
import com.example.shopdemo.entity.Shop;
import com.example.shopdemo.enums.ProductStateEnum;
import com.example.shopdemo.repository.ProductRepository;
import com.example.shopdemo.service.ProductCategoryServiceImpl;
import com.example.shopdemo.service.ProductServiceImpl;
import com.example.shopdemo.utils.CodeUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ：Anitaxiaofeng
 * @description：
 * @create 2018-01-21
 **/
@Controller
@RequestMapping("/shop")
@Slf4j
public class ProductManagementController {

    @Autowired
    private ProductServiceImpl productService;

    @Autowired
    private ProductCategoryServiceImpl productCategoryService;

    @Autowired
    private ProductRepository productRepository;



    @RequestMapping("/listProductsByShop")
    @ResponseBody
    public Map<String,Object> listProductByShop(HttpServletRequest request,@RequestParam("page")int page,@RequestParam("size")int size,
                                                @RequestParam(name = "productName",required = false)String productName,
                                                @RequestParam(name = "productCategoryId",required = false)Long productCategoryId){
        Map<String,Object> modelMap = new HashMap<>();
        Shop currentShop = (Shop) request.getSession().getAttribute("currentShop");
        Product productCondition = new Product();
        if(productName!=null){
            productCondition.setProductName(productName);
        }
        if(productCategoryId!=null){
            productCondition.setProductCategoryId(productCategoryId);
        }
        if(currentShop.getShopId()!=null){
        productCondition.setShopId(currentShop.getShopId());
        ProductExecution productExecution = productService.getProductList(productCondition,page,size);
        modelMap.put("productList",productExecution.getProductList());
        modelMap.put("success",true);
        modelMap.put("count",productExecution.getCount());
        } else{
            modelMap.put("success",false);
            modelMap.put("errMsg","ShopId is null");
        }
        return modelMap;
    }




    @RequestMapping(value = "/addProduct",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> addProduct(HttpServletRequest request, Product product,@RequestParam("file")MultipartFile[] imgAddrList, @RequestParam("verifyCodeActual")String verifyCodeActual){
        Map<String,Object> modelMap = new HashMap<>();
        if(!CodeUtil.checkVerifyCode(verifyCodeActual,request)){
            modelMap.put("success",false);
            modelMap.put("errMsg","验证码错误");
            return modelMap;
        }
        MultipartFile imgAddr = imgAddrList[0];
        List<MultipartFile> productImgList = new ArrayList<>();
        for(int i=1;i<imgAddrList.length;i++) {
            productImgList.add(imgAddrList[i]);
        }
        if(imgAddr==null||productImgList==null){
            modelMap.put("success",false);
            modelMap.put("errMsg","上传图片不能为空");
            return modelMap;
        }
        if(product!=null&&imgAddr!=null&&productImgList.size()>0){
            Shop currentShop = (Shop)request.getSession().getAttribute("currentShop");
            product.setShopId(currentShop.getShopId());
            ProductExecution productExecution = productService.addProduct(product,imgAddr,productImgList);
            if(productExecution.getState()== ProductStateEnum.SUCCESS.getKey()){
                modelMap.put("success",true);
            }else{
                modelMap.put("success",false);
                modelMap.put("errMsg",productExecution.getStateInfo());
            }
        }else{
            modelMap.put("success",false);
            modelMap.put("errMsg","请输入商品信息");
        }

        return modelMap;
    }


    @RequestMapping("/getProductById")
    @ResponseBody
    public Map<String,Object> getProductById(@RequestParam("productId")Long productId){
        Map<String,Object> modelMap = new HashMap<>();
        if(productId!=null){
            Product product = productService.getProductById(productId);
            List<ProductCategory> productCategoryList = productCategoryService.getByShopId(product.getShopId());
            modelMap.put("product",product);
            modelMap.put("productCategoryList",productCategoryList);
            modelMap.put("success",true);
        }else{
            modelMap.put("success",false);
            modelMap.put("errMsg","productId is null");
        }
        return modelMap;
    }

    @RequestMapping(value = "/modifyProduct",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> modifyProduct(HttpServletRequest request,Product product,@RequestParam(value = "file",required = false)MultipartFile[] imgAddrList,
                                            @RequestParam("verifyCodeActual")String verifyCodeActual){
        Map<String,Object> modelMap = new HashMap();
        if(!CodeUtil.checkVerifyCode(verifyCodeActual,request)){
            modelMap.put("success",false);
            modelMap.put("errMsg","验证码错误");
            return modelMap;
        }
        if(product!=null){
            Shop currentShop = (Shop)request.getSession().getAttribute("currentShop");
            product.setShopId(currentShop.getShopId());
            ProductExecution productExecution = new ProductExecution();
            if(imgAddrList.length==1){
                productExecution = productService.modifyProduct(product,imgAddrList[0],null);
            }else if(imgAddrList.length>1){
                MultipartFile imgAddr = imgAddrList[0];
                List<MultipartFile> productImgList = new ArrayList<>();
                for(int i=1;i<imgAddrList.length;i++) {
                    productImgList.add(imgAddrList[i]);
                }
                productExecution = productService.modifyProduct(product,imgAddr,productImgList);
            }else{
                productExecution = productService.modifyProduct(product,null,null);
            }
            if(productExecution.getState()==ProductStateEnum.SUCCESS.getKey()){
                modelMap.put("success",true);
            }else{
                modelMap.put("success",false);
                modelMap.put("errMsg",productExecution.getStateInfo());
            }
        }else{
            modelMap.put("success",false);
            modelMap.put("errMsg","请输入商品信息");
        }
        return modelMap;
    }

    /**
     * 下架改变enableStatus
     * @param productId
     * @return
     */
    @RequestMapping("/deleteProduct")
    @ResponseBody
    public Map<String,Object> deleteProduct(@RequestParam("productId")Long productId,@RequestParam("enableStatus")Integer enableStatus){
        Map<String,Object> modelMap = new HashMap<>();
        Product product = productService.getProductById(productId);
        product.setEnableStatus(enableStatus);
        Product productResult = productRepository.save(product);
        if(productResult!=null){
            modelMap.put("success",true);
        }else{
            modelMap.put("success",false);
        }
        return modelMap;
    }

}



